package com.hopedove.view;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.hopedove.net.R;

public class SimpleInputDialog extends DialogFragment {
    public static final String INPUT_TYPE = "EditTextInputType";
    private InputValueCallback mCallback;
    private Bundle extras;

    public SimpleInputDialog(InputValueCallback callback) {
        mCallback = callback;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Activity mActivity = getActivity();
        if (mActivity != null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
            LayoutInflater layoutInflater = mActivity.getLayoutInflater();
            final View view = layoutInflater.inflate(R.layout.simple_input_dialog, null);
            setEditInputMethod(view);
            String title = extras.getString("title");
            if (!TextUtils.isEmpty(title)) {
                TextView tv = view.findViewById(R.id.tv_title);
                tv.setText(title);
            }
            String data = extras.getString("value", "");
            final EditText et = view.findViewById(R.id.et_content);
            et.setText(data);
            et.requestFocus();

            builder.setView(view)
                    .setPositiveButton("确认", (dialog, which) -> {
                        String value = et.getText().toString();
                        if (mCallback != null) {
                            mCallback.onReceived(value, extras);
                        }
                    });

            return builder.create();
        }
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void setEditInputMethod(View content) {
        EditText editText = content.findViewById(R.id.et_content);
        editText.setInputType(getInputType());
    }

    private int getInputType() {
        final int defaultType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS;
        if (extras != null) {
            return extras.getInt(INPUT_TYPE, defaultType);
        }
        return defaultType;
    }


    public boolean isShowing() {
        Dialog dialog = getDialog();
        return dialog != null && dialog.isShowing();
    }

    public void show(FragmentActivity activity, Bundle bundle) {
        if (activity != null) {
            extras = bundle;
            FragmentManager manager = activity.getSupportFragmentManager();
            show(manager, "simple_input");
        }
    }


    public interface InputValueCallback {
        void onReceived(String value, Bundle bundle);
    }
}
